package com.liu.service;

import com.liu.mapper.UserMapper;
import com.liu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author: 刘浩然
 * @Date: 2020/3/24 21:40
 */
@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user =userMapper.loadUserByUsername(username);//从数据库中查询用户名
        if(user==null){
            throw new UsernameNotFoundException("账户不存在!请前往注册！");
        }
        user.setRoles(userMapper.getUserRoleByUid(user.getId()));//通过用户的id找到他的角色，然后封装他的角色
        return user;
    }
}
